﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Criterion;

namespace SmartProject.Entity
{
    public class ProductCategory : EntityBase
    {
        public ProductCategory()
        {
            Products = new List<Product>();
            this.OnEntityDeactivate += new EntityDeactivate(ProductCategory_OnEntityDeactivate);
            this.OnEntityDeleted += new EntityDeleted(ProductCategory_OnEntityDeleted);
        }

        private void ProductCategory_OnEntityDeleted(ISession session)
        {
            Delete(session, typeof(Product));
        }

        private void ProductCategory_OnEntityDeactivate(ISession session)
        {
            for (int i = 0; i < this.Products.Count; i++)
            {
                this.Products[i].Active = false;
            }
        }

        public virtual string Description { get; set; }

        public virtual DateTime Modified { get; set; }

        public virtual IList<Product> Products { get; set; }

        public static List<ProductCategory> GetProductCategories(ISession session)
        {
            IList<EntityBase> entities = Find(session, null, null, typeof(ProductCategory));
            if (entities != null)
            {
                return (from e in entities select e as ProductCategory).ToList();
            }
            return null;
        }

        /// <summary>
        /// Gets ProductCategory by Id.
        /// </summary>
        /// <param name="session">Session used in this request.</param>
        /// <param name="productCategoryId">Product category Id.</param>
        /// <returns>ProductCategory.</returns>
        public static ProductCategory GetProductCategory(ISession session, int productCategoryId)
        {
            return (ProductCategory)GetEntity(session, productCategoryId, typeof(ProductCategory));
        }

        /// <summary>
        /// Gets ProductCategory by description.
        /// </summary>
        /// <param name="session">Session used in this request.</param>
        /// <param name="description">Description of the ProductCategory.</param>
        /// <returns>ProductCategory.</returns>
        public static ProductCategory GetProductCategory(ISession session, string description)
        {
            IList<ICriterion> expressions = new List<ICriterion>();
            expressions.Add(Restrictions.Eq("Description", description));
            IList<EntityBase> entities = Find(session, expressions, null, typeof(ProductCategory));
            if (entities != null && entities.Count == 1)
            {
                return (ProductCategory)entities[0];
            }
            return null;
        }

        /// <summary>
        /// Deletes the inactive ProductCategory instances.
        /// </summary>
        /// <param name="session">Session used in this request.</param>
        public static void Clean(ISession session)
        {
            Delete(session, typeof(ProductCategory));
        }
    }
}
